package com.grgbanking.encryp.service;

import com.grgbanking.encryp.bean.KeyInfo;
import com.grgbanking.encryp.dao.UserRepository;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service("keyInfoService")
public class KeyInfoService {
    @Autowired
    private UserRepository userRepository;

    public KeyInfo getUserByID(Long id){
        return userRepository.findById(id).get();
    }

    public KeyInfo getByName(String name){
        return userRepository.findByName(name);
    }

    public Page<KeyInfo> findPage(){
        Pageable pageable = PageRequest.of(0, 10);
        return userRepository.findAll(pageable);
    }

    public Page<KeyInfo> find(Long maxId){
        Pageable pageable = PageRequest.of(0, 10);
        return userRepository.findMore(maxId,pageable);
    }

    public KeyInfo save(KeyInfo u){
        return userRepository.save(u);
    }

    public KeyInfo update(Long id,String name){
        KeyInfo user = userRepository.findById(id).get();
        user.setName(name+"_update");
        return userRepository.save(user);
    }
    public void processReceivedData(JsonNode data) {
        // 处理接收到的数据
        System.out.println("Processing data: " + data);
        // 具体业务逻辑
    }

    public int deleteByName(String keySN) {
        return userRepository.deleteByName(keySN);
    }

    public void updateDeletingByName(String keySN) {
        userRepository.updateDeletingByName(keySN,"D");
    }
}
